﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VFDDriver.Properties;

namespace VFDDriver
{
    public enum Alignment
    {
        Left,
        Right,
        Center,
        Justified
    }

    public class DisplayLineData
    {
        private long _scroll;

        private int _index;
        private string _currentValue;
        /// <summary>TODO: Add description here.</summary>
        public string CurrentValue
        {
            get { return _currentValue; }
        }

        private string _displayValue;
        /// <summary>TODO: Add description here.</summary>
        public string DisplayValue
        {
            get
            {
                Update();
                return _displayValue;
            }
        }

        private Alignment _align;
        /// <summary>TODO: Add description here.</summary>
        public Alignment Align
        {
            get { return _align; }
        }

        public DisplayLineData(string value, Alignment align)
        {
            _currentValue = value;
            _displayValue = value;
            _align = align;
            _index = 0;
            _scroll = DateTime.Now.Ticks;
        }

        public void Update()
        {
            string currentValue = _currentValue;

            currentValue = currentValue.Replace("${time}", DateTime.Now.Hour.ToString().PadLeft(2, '0') + ":" + DateTime.Now.Minute.ToString().PadLeft(2, '0') + ":" + DateTime.Now.Second.ToString().PadLeft(2, '0'));
            currentValue = currentValue.Replace("${date}", DateTime.Now.ToShortDateString());

            string line = currentValue;

            if (line.Length > Settings.Default.VFD_NrColumns)
            {
                long scrollDiff = DateTime.Now.Ticks - _scroll;
                if (scrollDiff >= Settings.Default.Data_ScrollTime * 10000)
                {
                    StringBuilder sb = new StringBuilder();

                    switch (_align)
                    {
                        case Alignment.Left:
                        case Alignment.Center:
                        case Alignment.Justified:
                            line = line + "  ";
                            if (_index >= line.Length)
                                _index = 0;
                            for (int i = _index; i < line.Length && sb.Length < Settings.Default.VFD_NrColumns; i++)
                            {
                                sb.Append(line[i]);
                            }

                            if (sb.Length < Settings.Default.VFD_NrColumns)
                            {
                                for (int j = 0; sb.Length < Settings.Default.VFD_NrColumns; j++)
                                {
                                    sb.Append(line[j]);
                                }
                            }

                            break;
                        case Alignment.Right:

                            line = "  " + line;
                            if (_index >= line.Length)
                                _index = 0;

                            for (int i = _index; i < line.Length && sb.Length < Settings.Default.VFD_NrColumns; i++)
                            {
                                sb.Insert(0, line[line.Length - 1 - i]);
                            }

                            if (sb.Length < Settings.Default.VFD_NrColumns)
                            {
                                for (int j = 0; sb.Length < Settings.Default.VFD_NrColumns; j++)
                                {
                                    sb.Insert(0, line[line.Length - 1 - j]);
                                }
                            }
                            break;
                        default:
                            break;
                    }

                    _displayValue = sb.ToString();
                    _index++;
                    _scroll = DateTime.Now.Ticks;
                }
            }
            else
            {
                switch (_align)
                {
                    case Alignment.Left:
                        _displayValue = line.PadRight(20, ' ');
                        break;
                    case Alignment.Right:
                        _displayValue = line.PadLeft(Settings.Default.VFD_NrColumns, ' ');
                        break;
                    case Alignment.Center:
                        int plus = Settings.Default.VFD_NrColumns - line.Length;
                        plus = plus / 2;
                        line = line.PadRight(line.Length + plus, ' ');
                        _displayValue = line.PadLeft(Settings.Default.VFD_NrColumns, ' ');
                        break;
                    default:
                        break;
                }
            }
            if (string.IsNullOrEmpty(_displayValue))
                _displayValue = string.Empty;
            else
                if (_displayValue.Length > 20)
                    _displayValue = _displayValue.Substring(0, Settings.Default.VFD_NrColumns);
        }
    }

}
